/*
 * JniCppFile.java  0.4.0 / Nov 6, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
package com.cute4j.tools.ng.jni;

import java.io.File;
import java.util.HashMap;

import com.thoughtworks.qdox.model.JavaClass;
import com.thoughtworks.qdox.model.JavaMethod;

import com.cute4j.tools.ng.AbstractNativeFile;
import com.cute4j.tools.ng.NativeFileFooter;
import com.cute4j.tools.ng.NativeFileHeader;
import com.cute4j.tools.ng.NativeFileParts;
import com.cute4j.tools.ng.Utils;

/**
 * JNI Implementation File.
 *
 * @author Slobodan Vrkacevic
 * @version 0.4.0
 */
public class JniCppFile extends AbstractNativeFile {

// Public Constructors. --------------------------------------------------------

  /**
   * Constructs new JniCppFile object.
   *
   * @param jniFolder the JNI folder
   * @param javaClass the qdox java class
   */
  public JniCppFile(File jniFolder, JavaClass javaClass) {
    super(jniFolder, Utils.getMangledClassName(javaClass) + ".cpp", javaClass);
  }

// Overridden Methods. ---------------------------------------------------------

  /**
   * {@inheritDoc}
   */
  protected NativeFileParts createParts() {
    NativeFileParts parts = new NativeFileParts();
    parts.add(new NativeFileHeader(this));
    parts.add(new JniCppFileStart(this, getJavaClass()));
    JavaMethod[] methods = getJavaClass().getMethods();
    HashMap methodsHash = new HashMap((2 * methods.length) + 1);
    HashMap overridenMethods = (HashMap)methodsHash.clone();
    for (int i = 0; i < methods.length; i++) {
      String methodName = methods[i].getName();
      if (!overridenMethods.containsKey(methodName)) {
        if (methodsHash.containsKey(methodName)) {
          overridenMethods.put(methodName, methodName);
        } else {
          methodsHash.put(methodName, methodName);
        }
      }
    }
    methodsHash.clear();
    for (int i = 0; i < methods.length; i++) {
      JavaMethod method = methods[i];
      if (method.isNative()) {
        boolean overridden = overridenMethods.containsKey(method.getName());
        parts.add(new JniFileMethodImplementation(this, method, overridden));
      }
    }
    parts.add(new NativeFileFooter(this));
    return parts;
  }

}
